<?php
/*
Plugin Name: NMedia Register User Plugin
Plugin URI: http://www.najeebmedia.com
Description: This plugin is developed for Company use
Version: 1.0
Author: Najeeb Ahmad
Author URI: http://www.najeebmedia.com/
*/

/*ini_set('display_errors',1);
error_reporting(E_ALL);*/


class nmUserManager {
	

	/*
	  data vars
	*/
	var $userID;
	var $username;
	var $password;
	var $email;
	var $response;
	var $error;
	var $posted_data;		//data posted by IPN/INS
	
	var $plugin_version;
	
	/*
	** grab keys from IPN
	*/
	var $metaKeys = array(  'vendor_id',
							'sale_id',
							'sale_date_placed',
							'vendor_order_id',
							'invoice_id',
							'payment_type',
							'list_currency',
							'cust_currency',
							'invoice_status',
							'fraud_status',
							'invoice_list_amount',
							'invoice_usd_amount',
							'invoice_usd_amount',
							'invoice_cust_amount',
							'customer_first_name',
							'customer_last_name',
							'customer_email',
							'customer_ip',
							'customer_ip_country',
							'item_name_1'
							);
							
	
	
	
	/**
	* constructor
	*/	 
	function nmUserManager() {
		
		//echo 'yes man';
	}
	
	
	/*
	** creating new user
	*/
	function addUser()
	{
		$this -> setBasicInfo();
		
		
		if(username_exists( $this->username ))
		{
			$this -> error = 'username already exists';
			$this -> response = false;
		}
		elseif(email_exists($this->email ))
		{
			$this -> error = 'email already exists';
			$this -> response = false;
		}
		else
		{
			$this->password = wp_generate_password( 12, false );
			$this -> userID = wp_create_user( $this->username, $this->password, $this->email);
			$this -> setUserDetail();
			
			$this -> response = true;
		}
		
	}
	
	/*
	** setting posted_data array, retreived from INS/IPN
	*/
	function setPostedData($data)
	{
		$this -> posted_data = $data;
	}
	
	/*
	** setting basic data for new user
	*/
	function setBasicInfo()
	{
		$this -> username 	= $this -> posted_data['customer_first_name'] . $this -> genRandomString();
		$this -> email 		= $this -> posted_data['customer_email'];
	}
	
	/*
	** setting up more detail as user meta
	*/
	
	function setUserDetail()
	{
		foreach($this -> metaKeys as $key)
		{
			//echo $this -> posted_data[$key]."<br />";
			update_user_meta($this -> userID, $key, $this -> posted_data[$key]);
		}
		
		
		/*$res = get_metadata('user', $this -> userID);
		
		echo '<pre>';
		print_r($res);
		echo '</pre>';*/
	}
	
	/*
	** getting random string to append to customer_frst_name to get unique username
	*/
	
	function genRandomString() {
	    $length = 5;
	    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
	    $string = "";    
	    for ($p = 0; $p < $length; $p++) {
	        $string .= $characters[mt_rand(0, strlen($characters))];
	    }
	    return $string;
	}
	
	
	/*
	** sending user an email with instructino/attachment
	*/
	
	function sendConfirmationEmail()
	{
		//date_default_timezone_set('America/Toronto');
		
		$php_mailer_path = dirname(__FILE__) . '/mailer/class.phpmailer.php';		
		
		require_once($php_mailer_path);
		
		$mail             = new PHPMailer();

		$body             = file_get_contents(plugins_url('email.html', __FILE__));
		
		$full_name = $this -> posted_data['customer_first_name'].' '.$this -> posted_data['customer_last_name'];
		$body             = eregi_replace("[\]",'',$body);
		$body             = str_replace("FULLNAME", $full_name, $body);
		//$body             = str_replace("USERNAME",$this -> username, $body);
		//$body             = str_replace("PASSWORD",'33SDD3',$body);
		//$body             = str_replace("PLUGIN",'NMedia File Uploader Pro',$body);
				
		/*$mail->IsSMTP(); // telling the class to use SMTP
		$mail->Host       = "mail.najeebmedia.com"; // SMTP server
		//$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
		                                           // 1 = errors and messages
		                                           // 2 = messages only
		$mail->SMTPAuth   = true;                  // enable SMTP authentication
		$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
		$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
		$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
		$mail->Username   = "sales@najeebmedia.com";  // GMAIL username
		$mail->Password   = "saaorglop8116"; */           // GMAIL password
		
		$mail -> IsSendmail();
		
		$mail->SetFrom('sales@najeebmedia.com', 'Sales Team');
		
		$mail->AddReplyTo("sales@najeebmedia.com","Sales Team");
		
		$mail->Subject    = "Your Order have been received - NMedia";
		
		//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
		
		$mail->MsgHTML($body);
		
		$address = $this -> email;
		$mail->AddAddress($address, $full_name);
		
		$address = "ceo@najeebmedia.com";
		$mail->AddAddress($address, "Najeeb Ahmad");
		
		
		
		$attachment = dirname(__FILE__).'/asset/'.$this -> plugin_version;
		//echo 'attachment '.$attachment;
		$mail->AddAttachment($attachment);      // attachment
		//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
		
		if(!$mail->Send()) {
		  echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
		  echo "Message sent!";
		}
		
	}
	
	
	/*
	** securing the member page
	*/
	function secureThisPage()
	{
		if ( !is_user_logged_in() ) { 
			echo '<script type="text/javascript">
			window.location = "'.wp_login_url( get_permalink() ).'"
			</script>';
		}		
	}
	
	/*
	** rendering forms based on shortcode parameter
	*/
	
	function renderForm($atts)
	{
		extract(shortcode_atts(array(
		  'form' 	=> '',
   		), $atts));
		
		
		
	}
	
	
}

//add_action('admin_menu', array('nmRealEstate', 'set_up_admin_page'));

/* shortcode for redering forms*/
add_shortcode('render-forms-nmedia', array('nmUserManager', 'renderForm'));
?>